/*!
 * MultiUpload for xheditor
 * @requires xhEditor
 *
 * @author Yanis.Wang<yanis.wang@gmail.com>
 * @site http://xheditor.com/
 * @licence LGPL(http://www.opensource.org/licenses/lgpl-license.php)
 *
 * @Version: 0.9.2 (build 100505)
 */
var swfu, selQueue = [];

function fileQueued(file) {
	
	//防止同名文件重复添加
	for (var i in selQueue) if (selQueue[i].name == file.name) {
		swfu.cancelUpload(file.id);
		layer.msg('这个文件已经上传过了', {icon: 2});
		return false;
	}
	
	//判断已经上传了几张
	i = 0;
	$("[name='article_thumb[]']").each(function(index, item){
		i++;
	});
	if(i>=10){
		layer.closeAll('loading');//关闭正在加载
		swfu.cancelUpload(file.id);//取消
		layer.msg('文章相册最多只能上传5张', {icon: 2});
		return false;
	}

	selQueue.push(file);
	if ($('#uploadlist').html().trim() == '') {
		cover = '封面';
	} else {
		cover = "&nbsp;";
	}
	var div = '<li id="show_' + file.id + '">';
	div += '<div class="displayimg" id="img_' + file.id + '"><img src="/Static/admin/js/swfupload/images/zoomloader.gif" style="width:16px;margin-top:20px;height:16px;"/></div>';
	div += '<a class="previous" href="javascript:void(0);"></a>';
	div += '<span class="front-cover">' + cover + '</span>';
	div += '<a class="next" href="javascript:void(0);"></a>';
	div += '<a class="delete" href="javascript:void(0);"></a>';
	div += '<input type="hidden" name="article_thumb[]" class="article_thumb" id="article_thumb_' + file.id + '" value="" />';
	div += '</li>';
	$('#uploadlist').append(div);
}


function fileQueueError(file, errorCode, message) //队列添加失败
{
	var errorName = '';
	switch (errorCode) {
	case SWFUpload.QUEUE_ERROR.QUEUE_LIMIT_EXCEEDED:
		errorName = "对不起，您最多可以上传 " + (this.settings.file_upload_limit) + " 个图片";
		break;
	case SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT:
		errorName = "选择的图片超过了当前大小限制：" + this.settings.file_size_limit;
		break;
	case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:
		errorName = "零大小文件";
		break;
	case SWFUpload.QUEUE_ERROR.INVALID_FILETYPE:
		errorName = "图片扩展名必需为：" + this.settings.file_types_description + " (" + this.settings.file_types + ")";
		break;
	default:
		errorName = "未知错误";
		break;
	}
	layer.msg(errorName, {icon: 2});
}


function fileDialogComplete(numFilesSelected, numFilesQueued) {
	try {
		if (numFilesQueued > 0) {
			this.startUpload();
		}
	} catch (ex) {
		this.debug(ex);
	}
}

//单文件上传开始
function uploadStart(file) 
{
	layer.load();//正在加载
}

//单文件上传进度
function uploadProgress(file, bytesLoaded, bytesTotal) 
{
	
}

//单文件上传成功
function uploadSuccess(file, serverData) {
	
	layer.closeAll('loading');//关闭正在加载
	
	var data = Object;
	try {
		eval("data=" + serverData);
	} catch (ex) {};
	if (data.ret == 0) {
		//成功上传
		var imgurl =  data.data.imgFile.savepath + data.data.imgFile.savename;
		var imgname = data.data.imgFile.name;
		addItem(imgurl, imgname, file);
		caozuo();
		layer.msg(data.msg, {icon: 1});

	} else {
		//上传失败
		$('#show_' + file.id).remove();
		layer.msg(data.msg, {icon: 2});
	}
	
	
}

//单文件上传错误
function uploadError(file, errorCode, message) 
{
	return message;
}

//文件上传周期结束
function uploadComplete(file) 
{
	if (swfu.getStats().files_queued > 0) swfu.startUpload();
	else uploadAllComplete();
}

function uploadAllComplete() {
	//layer.msg("文件上传完成", {icon: 1});
	layer.closeAll('loading');//关闭正在加载
}

//图片回传到网页
function addItem(url, path, file) {
	$("#img_" + file.id).html('<img src="'+UPYUN_DOMAIN+'' + url + '"/>');
	$("#article_thumb_" + file.id).val(url);
}

//绑定事件
function caozuo(){
	
	$('#uploadPic ul li a').on('click',function() {

		var o = $(this),
			pNode = o.parent(),
			v = $(this).attr('class');
		
		switch (v) {
		case 'delete':
			refNode = pNode['next']();
			if (pNode.find("span").html() == '封面') {
				refNode.find("span").html("封面");
			}
			pNode.remove();
			break;
		case 'previous':
		case 'next':
			var refNode = pNode[v == 'previous' ? 'prev' : 'next']();
			if (refNode[0] == null) {
				return false;
			}
			setItemPosition(pNode.parent(), true);
			var nItemTop = refNode.css('margin-left'),
				refItemTop = pNode.css('margin-left');
			pNode[v == 'previous' ? 'after' : 'before'](refNode); //交换位置
			pNode.animate({
				marginLeft: nItemTop
			}, 300);
			refNode.animate({
				marginLeft: refItemTop
			}, 300, function() {
				setItemPosition(pNode.parent());
			});
			break;
		}
	});
	
}

$(document).ready(function() {
	caozuo();
});

//封面设置
function setItemPosition(dvContainer, isAB) {
	var h = 0,
		eachItem;
	dvContainer.children().each(function() {
		eachItem = $(this);
		var nItemTop = eachItem.css('margin-left');
		if (nItemTop == '0px' && !isAB) {
			console.log(1);
			eachItem.find("span").html("封面");
		} else {
			eachItem.find("span").html("&nbsp;");
		}
		eachItem.css({
			'position': isAB ? 'absolute' : 'relative',
			'margin-left': isAB ? h : ''
		});
		if (isAB) h += 87;
	});
}